﻿using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class Base<T>:IBase<T> where T : class
    {
        public Base(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(T t)
        {
            Context.Set<T>().Add(t);
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(int id)
        {
            var m=Context.Set<T>().Find(id);
            Context.Set<T>().Remove(m); 
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 查全部
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<T> GetAll()
        {
            return Context.Set<T>();
        }
        /// <summary>
        /// 根据编号查一个信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetValue(int id)
        {
           return Context.Set<T>().Find(id);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdateAsync(T t)
        {
            Context.Set<T>().Update(t);
            return await Context.SaveChangesAsync();
        }
    }
}
